package users

import (
	"gin/common/lib/gorm"
	"gin/dao/entity"
	"gin/model/body"
	gorm2 "gorm.io/gorm"
)

// Add 添加用户
func Add(user entity.Users) int64 {
	gorm.GinDb.DB.Create(&user)
	return user.Id
}

// Get 获取用户详情
func Get(p body.UserSearchParam) (entity.Users, error) {
	users := entity.Users{}
	err := gorm.GinDb.DB.Model(&entity.Users{}).
		Where("id =?", p.Uid).Limit(1).
		Find(&users).Error
	return users, err
}

// GetUserByLogin 根据用户名获取用户
func GetUserByLogin(userLogin string) (entity.Users, error) {
	users := entity.Users{}
	err := gorm.GinDb.DB.Model(&entity.Users{}).
		Where("user_login=? OR mobile=? OR user_email=?", userLogin, userLogin, userLogin).Limit(1).
		First(&users).Error
	return users, err
}

// UpdateLocation 修改用户归属地
func UpdateLocation(id int64, location string) {
	gorm.GinDb.DB.Model(&entity.Users{}).
		Where("id = ?", id).
		Update("location", location)
}

// CheckUserLogout 检查用户状态
func CheckUserLogout(userLogin string) bool {
	user := entity.Users{}
	gorm.GinDb.DB.Model(&entity.Users{}).
		Where("(user_login = ? or mobile = ? or user_email = ?) and user_status = 9", userLogin, userLogin, userLogin).
		FirstOrInit(&user)
	return user.Id != 0
}

// CheckUserExist 校验用户
func CheckUserExist(userLogin string) bool {
	user := entity.Users{}
	gorm.GinDb.DB.Model(&entity.Users{}).
		Where("user_login = ? or mobile = ? or user_email = ?", userLogin, userLogin, userLogin).
		FirstOrInit(&user)
	return user.Id != 0
}

// UpdateField 修改用户参数
func UpdateField(uid int64, field map[string]interface{}) {
	gorm.GinDb.Model(&entity.Users{}).Where("id = ?", uid).Updates(&field)
}

// CheckRegistered 检验是否已经注册
func CheckRegistered(uid int64, email string) bool {
	user := entity.Users{}
	gorm.GinDb.Model(&entity.Users{}).Select("id").
		Where("where id = ? and user_login = ?", uid, email).Limit(1).Find(user)
	return user.Id != 0
}

// CheckEmailBind 校验邮箱是否绑定
func CheckEmailBind(uid int64, email string) bool {
	if err := gorm.GinDb.Model(&entity.Users{}).Select("id").
		Where("where id = ? and user_email = ?", uid, email).First(&entity.Users{}).Error; err != nil {
		return false
	} else {
		return true
	}
}

// CheckMobileBind 校验电话是否绑定
func CheckMobileBind(uid int64, mobile string) bool {
	if err := gorm.GinDb.Model(&entity.Users{}).Select("id").
		Where("where id = ? and user_email = ?", uid, mobile).First(&entity.Users{}).Error; err != nil {
		return false
	} else {
		return true
	}
}

// AddAttention 添加关注
func AddAttention(uid, toUid int64) {
	gorm.GinDb.Create(&entity.UsersAttention{
		Uid:      toUid,
		FollowId: uid,
	})
}

// DelAttention 取消关注
func DelAttention(id int64) {
	gorm.GinDb.Model(&entity.UsersAttention{}).Where("id", id).Delete(&entity.Users{})
}

// GetExist 是否关注过
func GetExist(uid, toUid int64) int64 {
	var id int64
	gorm.GinDb.Model(&entity.UsersAttention{}).Where("uid = ? and follow_Id = ?", toUid, uid).
		Pluck("id", &id)
	return id
}

// IsUsersNum 是否存在用户数量
func IsUsersNum(uid int64) {
	var id int64
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).Pluck("id", &id)
	if id == 0 {
		gorm.GinDb.Create(&entity.UsersNum{
			Uid: uid,
		})
	}
}

// AddFollowNum 添加粉丝数量
func AddFollowNum(uid int64) {
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).
		Update("follow_num", gorm2.Expr("follow_num + ?", 1))
}

// DelFollowNum 删除粉丝数量
func DelFollowNum(uid int64) {
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).
		Update("follow_num", gorm2.Expr("follow_num - ?", 1))
}

// AddAttentionNum 添加关注数量
func AddAttentionNum(uid int64) {
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).
		Update("attention_num", gorm2.Expr("attention_num + ?", 1))
}

// DelAttentionNum 删除关注数量
func DelAttentionNum(uid int64) {
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).
		Update("attention_num", gorm2.Expr("attention_num - ?", 1))
}

// GetNum 获取数量
func GetNum(uid int64) entity.UsersNum {
	info := entity.UsersNum{}
	gorm.GinDb.Model(&entity.UsersNum{}).Where("uid = ?", uid).Limit(1).Find(&info)
	return info
}

// GetFansList 获取粉丝列表
func GetFansList(p body.ToUidListParam) []*entity.UsersAttentionList {
	list := make([]*entity.UsersAttentionList, 0)
	gorm.GinDb.Table("cmf_users_attention as a").Select("a.*,u.user_nickname,u.avatar").
		Joins("left join cmf_users u on u.id = a.follow_id").
		Where("uid = ?", p.ToUid).
		Offset(p.GetOffset()).Limit(p.GetPage()).Find(&list)
	return list
}

// GetAttentionList 获取关注列表
func GetAttentionList(p body.ToUidListParam) []*entity.UsersAttentionList {
	list := make([]*entity.UsersAttentionList, 0)
	gorm.GinDb.Table("cmf_users_attention as a").Select("a.*,u.user_nickname,u.avatar").
		Joins("left join cmf_users u on u.id = a.uid").
		Where("follow_id = ?", p.ToUid).
		Offset(p.GetOffset()).Limit(p.GetPage()).Find(&list)
	return list
}

// IsCorrelation 判断是否互相关注
func IsCorrelation(uid int64, toUid []int64) map[int64]int {
	uids := make([]int64, 0)
	gorm.GinDb.Table("cmf_users_attention").Where("uid in ? and follow_id = ?", toUid, uid).
		Pluck("uid", &uids)
	uidMap := make(map[int64]int)
	for _, v := range uids {
		uidMap[v] = 1
	}
	return uidMap
}

// GetAttentionIds 获取关注id
func GetAttentionIds(uid int64) (uids []int64) {
	gorm.GinDb.Table("cmf_users_attention").Where("follow_id = ?", uid).Pluck("uid", &uids)
	return uids
}
